package com.itany.utils;

import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class HttpClientUtils {
    /**
     * http get请求方法
     * @param url
     * @return
     */
    public static String doGet(String url){
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String result = "";
        CloseableHttpResponse response = null;



        try {

            /**
             * 使用代理IP
             */

            //设置代理IP与端口号与请求类型
       /*     HttpHost proxy = new HttpHost("118.99.100.130",8080,"HTTP");
            RequestConfig requestConfig = RequestConfig.custom()
                    .setProxy(proxy)
                    .setConnectTimeout(100000)//连接主机的超时时间
                    .setSocketTimeout(100000)//客户端与服务端数据交互时间
                    .setConnectionRequestTimeout(100000)//主机读取数据的超时时间
                    .build();
            httpGet.setConfig(requestConfig);*/
            HttpGet httpGet = new HttpGet(url);
            response = httpClient.execute(httpGet);
            if(response.getStatusLine().getStatusCode()== HttpStatus.SC_OK){
                result = EntityUtils.toString(response.getEntity(), Charset.defaultCharset());
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            close(httpClient,response);
        }

        return result;
    }


    /**
     * http post请求方法
     * @return
     */
    public static String doPost(String url, Map<String,String> params){

        //创建httpclient客户端对象
        CloseableHttpClient httpClient = HttpClients.createDefault();

        //结果集
        String result = "";
        //响应对象声明
        CloseableHttpResponse response = null;
        try {
            //构建请求
            HttpPost httpPost = new HttpPost(url);

            //构建参数
            if(params!=null){
                List<NameValuePair> paramList = new ArrayList<NameValuePair>();
                for(Map.Entry<String,String> param : params.entrySet()){
                    paramList.add(new BasicNameValuePair(param.getKey(),param.getValue()));
                }

                //声明一个模拟form表单对象
                UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(paramList, Charset.defaultCharset());
                httpPost.setEntity(formEntity);
            }

            //执行http请求
            response = httpClient.execute(httpPost);
            //判断返回状态
            if(response.getStatusLine().getStatusCode()== HttpStatus.SC_OK){
                result = EntityUtils.toString(response.getEntity(),Charset.defaultCharset());
            }


        }catch (Exception e){
            e.printStackTrace();
        }finally {
            close(httpClient,response);
        }

        return result;

    }

    /**
     * 关闭连接方法
     * @param httpClient
     * @param response
     */
    private static void close(CloseableHttpClient httpClient, CloseableHttpResponse response){

        try {

            if(response!=null){
                response.close();
            }

            if(httpClient!=null){
                httpClient.close();
            }

        }catch (Exception e){
            e.printStackTrace();
        }


    }
}
